#include "common.h"
#include <vector>
#include <queue>

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> ans;
        queue<TreeNode*> q;

        if(root == NULL) return ans;
        q.push(root);

        while(!q.empty())
        {
          vector<int> temp;
          int size = q.size();

          while(size > 0)
          {
            TreeNode* node = q.front();
            temp.push_back(node->val);
            q.pop();

            if(node->left != NULL)
            {
              q.push(node->left);
            }
            if(node->right != NULL)
            {
              q.push(node->right);
            }

            size -= 1;
          }
          ans.push_back(temp);
        }
        return ans;
    }
};
